//
//  YDNetWorkTools.swift
//  AFN网络封装
//
//  Created by suibian on 16/8/10.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
import AFNetworking

//网络请求枚举
enum YDNetWorkToolsMethod:String{

    case GET = "GET"
    case PSOT = "PSOT"

}

class YDNetWorkTools: AFHTTPSessionManager {
    
    
    static let shareTools:YDNetWorkTools = {
        let tools = YDNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
    
    
        return tools
    }()

    
//    网络请求公共数据
    func request(method:YDNetWorkToolsMethod,urlString:String,parameters:AnyObject?,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        if method == .GET{
        GET(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
            success(response: response)
            }, failure: { (_, error) -> Void in
                failure(error:error)
                
                
                
        })
        }else{
            
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                success(response: response)
                }, failure: { (_, error) -> Void in
                    failure(error:error)

            
            })
        
        }
    }
    

}

//微博首页数据
extension YDNetWorkTools{
    func homeLoadData(sinceId:Int64,maxId:Int64, success:(response: AnyObject?)->() ,failure:(error:NSError) ->()){
//    请求的urlSting
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        请求参数
        let params = ["access_token":
            YDUserAccountViewModel.sharedTools.useAccount!.access_token!,
//            数据库那边不会有int64的类型 所以要转成字符串
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"

        ]
        request(YDNetWorkToolsMethod.GET, urlString: urlString, parameters:params, success: success, failure: failure)
    
    
    }



}


//授权界面接口
extension YDNetWorkTools {
    func oauthLoadUsetAcount(code:String,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        //    请求urlstring
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //        请求参数
        
        let params = [
            "client_id": WBAppKey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
            
            
        ]
        request(YDNetWorkToolsMethod.PSOT, urlString: urlString, parameters: params, success: success, failure: failure)
    
    }
    
//    请求个人信息
    func oauthLoadUserAccount(userAccount:YDUserAccountModel,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        //        请求url
        let urlString = "https://api.weibo.com/2/users/show.json"
       
        //    请求参数
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
    
    request(YDNetWorkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    
    }
}




//  请求天气接口
extension YDNetWorkTools{
    func getWeatherData(success:(response:AnyObject?)->(),failure:(error:NSError)->()){
    
    let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        request(YDNetWorkToolsMethod.GET, urlString: urlString, parameters: nil, success: success, failure: failure)
    
    }

}
// 发布微博的接口
extension YDNetWorkTools{
//    文字微博
    func composeUpdate(status:String,success:(response: AnyObject?)->() ,failure:(error:NSError) ->()){
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token":YDUserAccountViewModel.sharedTools.access_token!,
            "status":status
            
        ]
//        发送请求
        YDNetWorkTools.shareTools.request(YDNetWorkToolsMethod.PSOT, urlString: urlString, parameters: params, success: success, failure: failure)

    }
        //    发布图片微博
    func composeUpload(status:String,images:[UIImage],success:(response: AnyObject?)->() ,failure:(error:NSError) ->()){
            let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
            let params = [
                "access_token":YDUserAccountViewModel.sharedTools.access_token!,
                "status":status
            ]
//       发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            let data = UIImagePNGRepresentation(images.first!)!
            formdata.appendPartWithFileData(data, name: "pic", fileName: "xxyy", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) -> Void in
                success(response: response)
            }) { (_, error) -> Void in
                failure(error: error)
        }
    }
}



